#!/bin/bash

echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"
echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"
echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"

echo "-------- 开始尝试安装魔笔马良环境 2 -----------"
mbmlpath=$HOME/Desktop/mtai_workspace/MobiMaliangSDK
workpath=$HOME/Desktop/mtai_workspace
# 拉取魔笔马良官方镜像
cd $mbmlpath
sudo bash install.sh

# 检查返回状态
if [ $? -eq 0 ]; then
    echo "魔笔马良官方镜像拉取成功。"
else
    echo "拉取镜像时出现错误，请检查安装脚本和网络连接。"
    echo "请检查是否将 install.sh 文件里的账户和密码替换为你的开发者账户密码"
    echo "比如: docker login -u 'Abc' -p 'password' registry.mthreads.com"
    break
fi

# 启动魔笔马良镜像
sudo docker run -id --name mtai_workspace --privileged -e MTHREADS_VISIBLE_DEVICES=all -p 1001:1001 -p 1002:1002 -p 1003:1003 -p 1004:1004 -p 1005:1005 -v $workpath:/mtai_workspace:rw --shm-size 64G registry.mthreads.com/mcconline/musa-pytorch-release-public:v1.0.0
# 进入魔笔马良镜像
# --------------------------------------------------------
# 在容器中执行多个命令
docker exec -it mtai_workspace /bin/bash -c "\
    cd /mtai_workspace/MobiMaliangSDK/ && \
    bash install_inside_docker.sh
"
# 上面代码如果不行,则手动输入
#  cd /mtai_workspace/MobiMaliangSDK/
#  bash install_inside_docker.sh
# --------------------------------------------------------

# --------------------------------------------------------
docker exec -it mtai_workspace /bin/bash -c "\
    apt-get update && apt-get install ffmpeg libsm6 libxext6 -y && \
    pip install -r requirements.txt --no-deps && \
    mkdir -p ~/.cache/huggingface/hub/ && \
    rm -rf ~/.cache/huggingface/hub/models--openai--clip-vit-large-patch14/ && \
    cp -r models/tools/models--openai--clip-vit-large-patch14/ ~/.cache/huggingface/hub/
"
# 上面代码如果不行,则手动输入
# apt-get update && apt-get install ffmpeg libsm6 libxext6 -y
# pip install -r requirements.txt --no-deps
# mkdir -p ~/.cache/huggingface/hub/;rm -rf ~/.cache/huggingface/hub/models--openai--clip-vit-large-patch14/;cp -r models/tools/models--openai--clip-vit-large-patch14/ ~/.cache/huggingface/hub/
# --------------------------------------------------------

# --------------------------------------------------------
docker exec -it mtai_workspace /bin/bash -c "
    cd /mtai_workspace/MobiMaliangSDK/ && \
    streamlit run frontend/main.py --server.port 1001
"
# 上面代码如果不行,则手动输入
#   cd /mtai_workspace/MobiMaliangSDK/ && \
#   streamlit run frontend/main.py --server.port 1001
# --------------------------------------------------------

echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"
echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"
echo "---------- 如果不行的,推荐从本教程的手动安装地6步开始手动安装 ----------"
